package fr.castorflex.EnergySavingWidget;

import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.net.Uri;
import android.provider.Settings;
import android.util.Log;
import android.widget.RemoteViews;

public class EnergySavingWidget extends AppWidgetProvider {

	public static String CLICK_ON_WIDGET = "click on widget !";

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		// TODO Auto-generated method stub
		Log.i(this.getClass().getName(), "onupdate started");

		RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
				R.layout.main);

		Intent active = new Intent(context, EnergySavingWidget.class);
		active.setAction(CLICK_ON_WIDGET);
		PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context,
				0, active, 0);
		remoteViews
				.setOnClickPendingIntent(R.id.imageView, actionPendingIntent);

		appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
		super.onUpdate(context, appWidgetManager, appWidgetIds);

	}

	@Override
	public void onReceive(Context context, Intent intent) {
		final String action = intent.getAction();
		Log.i(this.getClass().getName(), "onreceive started");
		if (action.equals(CLICK_ON_WIDGET)) {
			passerModeEco(context);
		}

		super.onReceive(context, intent);
	}

	public void passerModeEco(Context context) {
		Log.i(this.getClass().getName(), "modeEco started");
		stopConnections(context);
		killAllTasks(context);
	}

	public void killAllTasks(Context context) {
		Log.i(this.getClass().getName(), "killAllTasks started");

		ActivityManager mgr = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);

		List<ActivityManager.RunningAppProcessInfo> procList = mgr
				.getRunningAppProcesses();
		Log.i(context.getClass().toString(), procList.size() + " taille ");
		if (procList != null) {
			for (RunningAppProcessInfo info : procList) {
				if (info.pid != android.os.Process.myPid()) {
					mgr.killBackgroundProcesses(info.processName);
					Log.i(context.getClass().toString(), info.processName
							+ " killed !");
				}
			}
		}

	}

	public void stopConnections(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		cm.stopUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
				"android.net.conn.CONNECTIVITY_CHANGE");
		cm.stopUsingNetworkFeature(ConnectivityManager.TYPE_WIFI,
				"android.net.conn.CONNECTIVITY_CHANGE");
		BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
		if(adapter != null)
			adapter.disable();
		stopGPS(context);

	}

	public void stopGPS(Context c) {
		String provider = Settings.Secure.getString(c.getContentResolver(),
				Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

		if (provider.contains("gps")) {
			final Intent poke = new Intent();
			poke.setClassName("com.android.settings",
					"com.android.settings.widget.SettingsAppWidgetProvider");
			poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
			poke.setData(Uri.parse("3"));
			c.sendBroadcast(poke);

		}

	}

}
